﻿
/*!
 * 产业链接
*/
var getHYJsonTree = function (data, pcode) {
    var itemArr = [];
    for (var i = 0; i < data.length; i++) {
        var node = data[i];
        if (node.pcode == pcode) {
            var newNode = { code: node.code, text: node.text, nodes: getJsonTree(data, node.code) };
            itemArr.push(newNode);
        }
    }
    return itemArr;
}

//Tab切换
function NavTabQH(bs, xh) {
    $("td[name*=" + bs + "Tab]").attr("class", "Tab_wbj");
    $("td[name*=" + bs + "Tab" + xh + "]").attr("class", "Tab_ybj");
    $("div[name*=" + bs + "Div]").hide();
    $("div[name*=" + bs + "Div" + xh + "]").show();
}

var Tree_HY_Json = [];
var LS_Tree_HY_Json = [];
var HY_Tree_Sel_Node = null;

//产业链查询参数 areatype：区域类型(1、行政区,2、功能区,3、产业园区)  showtype：显示方式(1、聚集图 2、麻点图3、热力图),行业编码，上游编码，下游编码，细分指标
var CYLQParams_Json = { areatype: "1", areaname: "", areacode: "", arealevel: 3, hycode: "", sycode: "", xycode: "", xfzbcode: "" };
var CY_Data_Json = [];
var SY_Data_Json = [];
var XY_Data_Json = [];
var CY_GridLayer = null;
var SY_GridLayer = null;
var XY_GridLayer = null;


//初始化产业链
function Init_CYL() {
    Fill_HYTree();
    Fill_XFZB();
    Fill_Area();
    Init_Layer();
    GetXFZB(0);
    Selected_XZQH_Tree(3, "430100000000", "长沙市");
}

//初始化产业链细分指标
function Fill_XFZB() {
    var strHtml = "";
    for (var i = 0; i < CYL_XFZB_Json.length; i++) {
        strHtml += '<option value="' + CYL_XFZB_Json[i].code + '">' + CYL_XFZB_Json[i].name + '</option>';
    }
    $("#Select_XFZB").html(strHtml);
}

//初始化区域
function Fill_Area() {
    Area_XZQ_Json = getJsonTree(LS_Area_XZQ_Json, '430100000000');
    DelNullNode(Area_XZQ_Json);
    var defareatext = "";
    var defareacode = "";
    var defarealevel = 3;
    try {
        defareatext = Area_XZQ_Json[0].text;
        defareacode = Area_XZQ_Json[0].code.substr(0, (defarealevel - 1) * 2);
    }
    catch (ex) {
        defareatext = "长沙市";
    }
    finally {
        $("#Nav_QYXZ").html(defareatext + '<span class="caret"></span>');
        CYLQParams_Json.areatype = "1";
        CYLQParams_Json.arealevel = defarealevel;
        CYLQParams_Json.areaname = defareatext;
        CYLQParams_Json.areacode = defareacode;

    }

    $('#AreaTreeView1').treeview({
        data: Area_XZQ_Json,
        onNodeSelected: function (event, node) {
            Selected_XZQH_Tree(node.level, node.code, node.text);
            $('#AreaTreeView1').treeview('unselectNode', node.nodeId, { silent: true });
        }
    });

    Area_GNQ_Json = [];
    var dsname = "DATA";
    var dtname = "ST_R_SH";
    var url = Area_Query_Params.query_url;
    var strcode = "";

    var queryParam, queryBySQLParams, queryBySQLService;
    queryParam = new SuperMap.REST.FilterParameter({
        name: dtname + "@" + dsname,
        attributeFilter: strcode
    }),
    queryBySQLParams = new SuperMap.REST.GetFeaturesBySQLParameters({
        queryParameter: queryParam,
        datasetNames: [dsname + ":" + dtname],
        toIndex: 30
    }),
    queryBySQLService = new SuperMap.REST.GetFeaturesBySQLService(url, {
        eventListeners: {
            "processCompleted": function QueryAreaCompleted(queryEventArgs) {
                var i, len, feature, result = queryEventArgs.result;
                if (result && result.features) {
                    var ArrGeo = result.features;
                    for (var i = 0; i < ArrGeo.length; i++) {
                        var LSObj = {
                            "code": ArrGeo[i].attributes["QH_CODE"],
                            "text": ArrGeo[i].attributes["QH_NAME"],
                            "level": "1",
                            "SMX": (parseFloat(ArrGeo[i].attributes["SMSDRIW"]) + parseFloat(ArrGeo[i].attributes["SMSDRIE"])) / 2,
                            "SMY": (parseFloat(ArrGeo[i].attributes["SMSDRIN"]) + parseFloat(ArrGeo[i].attributes["SMSDRIS"])) / 2
                        }
                        Area_GNQ_Json.push(LSObj);
                    }
                    $('#AreaTreeView2').treeview({
                        data: Area_GNQ_Json,
                        onNodeSelected: function (event, node) {
                            Selected_GNPQ_Tree(1, node.code, node.text);
                            $('#AreaTreeView2').treeview('unselectNode', node.nodeId, { silent: true });
                        }
                    });
                }
            },
            "processFailed": function processFailed(e) { }
        }
    });
    queryBySQLService.processAsync(queryBySQLParams);

    Area_CYYQ_Json = [];
    dsname = "DATA";
    dtname = "ST_R_XA";
    url = Area_Query_Params.query_url;
    strcode = "";
    var queryParam_1, queryBySQLParams_1, queryBySQLService_1;
    queryParam_1 = new SuperMap.REST.FilterParameter({
        name: dtname + "@" + dsname,
        attributeFilter: strcode
    }),
    queryBySQLParams_1 = new SuperMap.REST.GetFeaturesBySQLParameters({
        queryParameter: queryParam,
        datasetNames: [dsname + ":" + dtname],
        toIndex: 30
    }),
    queryBySQLService_1 = new SuperMap.REST.GetFeaturesBySQLService(url, {
        eventListeners: {
            "processCompleted": function QueryAreaCompleted(queryEventArgs) {
                var i, len, feature, result = queryEventArgs.result;
                if (result && result.features) {
                    var ArrGeo = result.features;
                    for (var i = 0; i < ArrGeo.length; i++) {
                        var LSObj = {
                            "code": ArrGeo[i].attributes["QH_CODE"],
                            "text": ArrGeo[i].attributes["QH_NAME"],
                            "level": "1",
                            "SMX": (parseFloat(ArrGeo[i].attributes["SMSDRIW"]) + parseFloat(ArrGeo[i].attributes["SMSDRIE"])) / 2,
                            "SMY": (parseFloat(ArrGeo[i].attributes["SMSDRIN"]) + parseFloat(ArrGeo[i].attributes["SMSDRIS"])) / 2
                        }
                        Area_CYYQ_Json.push(LSObj);
                    }
                    $('#AreaTreeView3').treeview({
                        data: Area_CYYQ_Json,
                        onNodeSelected: function (event, node) {
                            Selected_CYYQ_Tree(1, node.code, node.text);
                            $('#AreaTreeView3').treeview('unselectNode', node.nodeId, { silent: true });
                        }
                    });
                }
            },
            "processFailed": function processFailed(e) { }
        }
    });
    queryBySQLService_1.processAsync(queryBySQLParams_1);
}

//选择行政区划树
function Selected_XZQH_Tree(level, code, text) {
    var level = level;//parseInt(node.level);
    var code = code.substr(0, (level - 1) * 2);
    if (level == 5) {
        code = code.substr(0, 9);
    }
    if (level == 6) {
        code = code.substr(0, 12);
    }
    var text = text;//node.text;

    $("#Nav_QYXZ").html(text + '<span class="caret"></span>');
    $("#Nav_QYXZ").attr("title", text);
    CYLQParams_Json.areatype = "1";
    CYLQParams_Json.arealevel = level;
    CYLQParams_Json.areaname = text;
    CYLQParams_Json.areacode = code;
    $("#CYL_Area_Panal").hide();
    Area_Query_Params.dsname = "DATA";
    if (level == 2) {
        Area_Query_Params.dtname = "ST_R_SH";
        Area_Query_Params.strcode = "";
    }
    if (level == 3) {
        Area_Query_Params.dtname = "ST_R_XN";
        Area_Query_Params.strcode = "QH_CODE like '" + CYLQParams_Json.areacode + "%'";
    }
    if (level == 4) {
        Area_Query_Params.dtname = "ST_R_XA";
        Area_Query_Params.strcode = "QH_CODE like '" + CYLQParams_Json.areacode + "%'";
    }
    if (level == 5 || level == 6) {
        Area_Query_Params.dtname = "ST_R_CN";
        Area_Query_Params.strcode = "QH_CODE like '" + CYLQParams_Json.areacode + "%'";
    }
    Query_Area();
    Query_CYL();
}

//选择功能区
/*function Selected_GNPQ_Tree(level, code, text) {
    $("#Nav_QYXZ").html(text + '<span class="caret"></span>');
    $("#Nav_QYXZ").attr("title", text);
    CYLQParams_Json.areatype = "2";
    CYLQParams_Json.arealevel = level;
    CYLQParams_Json.areaname = text;
    CYLQParams_Json.areacode = code;
    $("#CYL_Area_Panal").hide();

    var strCode = "QH_CODE='" + CYLQParams_Json.areacode + "'";
    Area_Query_Params.dsname = "DATA";
    Area_Query_Params.dtname = "ST_R_GNPQ";
    Area_Query_Params.strcode = strCode;

    Query_Area();
    Query_CYL();
}*/

//选择产业园区
/*
function Selected_CYYQ_Tree(level, code, text) {
    $("#Nav_QYXZ").html(text + '<span class="caret"></span>');
    $("#Nav_QYXZ").attr("title", text);
    CYLQParams_Json.areatype = "3";
    CYLQParams_Json.arealevel = level;
    CYLQParams_Json.areaname = text;
    CYLQParams_Json.areacode = code;
    $("#CYL_Area_Panal").hide();

    var strCode = "QH_CODE='" + CYLQParams_Json.areacode + "'";
    Area_Query_Params.dsname = "DATA";
    Area_Query_Params.dtname = "ST_R_CYYQ";
    Area_Query_Params.strcode = strCode;

    Query_Area();
    Query_CYL();
}
*/


//区域选择控制
function Open_Area() {
    if ($("#CYL_Area_Panal").is(':hidden')) {
        $("#CYL_Area_Panal").show();
    }
    else {
        $("#CYL_Area_Panal").hide();
    }
}

//初始化行业树
function Fill_HYTree() {
    $.ajax({
        url: window.login_url.system_base_url + "/app/IndustryAnalysis/findIndustroyListes",
        type: 'post',
        data: {},
        cache: false,
        contentType:"application/x-www-form-urlencoded",
        success: function (text) {
            var info = eval("(" + text + ")");
            LS_Tree_HY_Json = info.data;
            Tree_HY_Json = getHYJsonTree(LS_Tree_HY_Json, '0');
            DelNullNode(Tree_HY_Json);
            $('#HYTree').treeview({
                levels: 1,
                data: Tree_HY_Json,
                onNodeSelected: function (event, node) {
                    if (node.code.length > 2) {
                        CYLQParams_Json.hycode = node.code;
                        Query_CYL();
                        HY_Tree_Sel_Node = node.nodeId;
                    }
                    else {
                        ClearAllLayer();
                        var nodeid = node.nodeId;
                        if (nodeid != null) {
                            $('#HYTree').treeview('unselectNode', nodeid, {
                                silent: true
                            });
                            nodeid = null;
                        }
                    }
                }
            });
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //alert(jqXHR.responseText);
        }
    });
}

function Init_Layer() {
    CY_GridLayer = new SuperMap.Layer.HeatMapFastLayer("产业");
    SY_GridLayer = new SuperMap.Layer.HeatMapFastLayer("上游");
    XY_GridLayer = new SuperMap.Layer.HeatMapFastLayer("下游");
    map.addLayers([Area_Layer,SY_GridLayer, XY_GridLayer]);
}


//细分指标展示
function GetXFZB(xh) {
    var obj = $("#Select_XFZB")[0];
    var val = obj.options[xh].value;
    var text = obj.options[xh].text;
    CYLQParams_Json.xfzbcode = val;

    CY_GridLayer.removeAllFeatures();
    SY_GridLayer.removeAllFeatures();
    XY_GridLayer.removeAllFeatures();
    //Draw_CYHeat();
    Query_SY();
    Query_XY();
}

//产业链查询
function Query_CYL() {
    CY_GridLayer.removeAllFeatures();
    SY_GridLayer.removeAllFeatures();
    XY_GridLayer.removeAllFeatures();
    CY_Data_Json = [];
    SY_Data_Json = [];
    XY_Data_Json = [];
    CYLQParams_Json.sycode = "";
    CYLQParams_Json.xycode = "";

    $.ajax({
        url: window.login_url.system_base_url + "/app/IndustryAnalysis/findData",
        type: 'post',
        data: CYLQParams_Json,
        cache: false,
        success: function (text) {
            if (text != "" && text != null) {
                var info = eval("(" + text + ")");
                CY_Data_Json = info.data;
                if (info.data_sxy.length!=0)
                {
                    CYLQParams_Json.sycode = info.data_sxy[0]["S00001"];
                    CYLQParams_Json.xycode = info.data_sxy[0]["S00002"];
                }
                Draw_CYHeat();
                Query_SY();
                Query_XY();
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //alert(jqXHR.responseText);
        }
    });
}

//绘制产业热力图
function Draw_CYHeat() {
    var heatFeatures = [];
    var cenx = 0, ceny = 0;
    var point = null;
    var ZB = CYLQParams_Json.xfzbcode;
    for (var i = 0; i < CY_Data_Json.length; i++) {
        cenx = CY_Data_Json[i].SMX;
        ceny = CY_Data_Json[i].SMY;
        point = new SuperMap.Geometry.Point(cenx, ceny);
        var attrs = {};
        /*第一个参数是最内层的，后面依次直到最外层*/
        var colors = ["#8DE6E6","#7CE37D","#E1E171","#dc3545"];
        attrs[ZB] = CY_Data_Json[i][ZB];
        var fea = new SuperMap.Feature.Vector(point, attrs);
        heatFeatures.push(fea);
    }
    CY_GridLayer.radius = 30;
    CY_GridLayer.colors = colors;
    CY_GridLayer.featureWeight = ZB;


    if (CY_Data_Json.length < 10000) {
        CY_GridLayer.addFeatures(heatFeatures);
        map.addLayers([CY_GridLayer]);
    }

}

//上游查询
function Query_SY() {
    if ($('#Show_SYHY').is(':checked')) {
        SY_GridLayer.setVisibility(true);
        if (CYLQParams_Json.sycode != "") {
            if (SY_Data_Json.length > 0) {
                Draw_SYHeat();
            }
            else {
                $.ajax({
                    url: "/app/IndustryAnalysis/findUpIndustroy/",
                    type: 'post',
                    data: CYLQParams_Json,
                    cache: false,
                    success: function (text) {
                        if (text != "" && text != null) {
                            var info = eval("(" + text + ")");
                            SY_Data_Json = info.data;
                            Draw_SYHeat();
                        }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        //alert(jqXHR.responseText);
                    }
                });
            }
        }
    }
    else {
        SY_GridLayer.setVisibility(false);
    }
    
}

//绘制上游热力图
function Draw_SYHeat() {
    var heatFeatures = [];
    var cenx = 0, ceny = 0;
    var point = null;
    var ZB = CYLQParams_Json.xfzbcode;
    var colors = ["#37BEFF", "#00699B"];
    //var colors = ["#AAF0E9", "#769A31"];
       
    for (var i = 0; i < SY_Data_Json.length; i++) {
        cenx = SY_Data_Json[i].SMX;
        ceny = SY_Data_Json[i].SMY;
        point = new SuperMap.Geometry.Point(cenx, ceny);
        var attrs = {};
        attrs[ZB] = SY_Data_Json[i][ZB];
        var fea = new SuperMap.Feature.Vector(point, attrs);
        heatFeatures.push(fea);
    }
    SY_GridLayer.radius = 30;
    SY_GridLayer.colors = colors
    SY_GridLayer.featureWeight = ZB;
    SY_GridLayer.addFeatures(heatFeatures);
}

//下游查询
function Query_XY() {
    if ($('#Show_XYHY').is(':checked')) {
        XY_GridLayer.setVisibility(true);
        if (CYLQParams_Json.xycode != "") {
            if (XY_Data_Json.length > 0) {
                Draw_XYHeat();
            }
            else {
                $.ajax({
                    url: "/app/IndustryAnalysis/findDownIndustroy/",
                    type: 'post',
                    data: CYLQParams_Json,
                    cache: false,
                    success: function (text) {
                        if (text != "" && text != null) {
                            var info = eval("(" + text + ")");
                            XY_Data_Json = info.data;
                            Draw_XYHeat();
                        }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        //alert(jqXHR.responseText);
                    }
                });
            }
        }
    }
    else {
        XY_GridLayer.setVisibility(false);
    }
}

//绘制下游热力图
function Draw_XYHeat() {
    var heatFeatures = [];
    var cenx = 0, ceny = 0;
    var point = null;
    var ZB = CYLQParams_Json.xfzbcode;
    var colors = ["#FFFFA4", "#FDDD3D"];
    for (var i = 0; i < XY_Data_Json.length; i++) {
        cenx = XY_Data_Json[i].SMX;
        ceny = XY_Data_Json[i].SMY;
        point = new SuperMap.Geometry.Point(cenx, ceny);
        var attrs = {};
        attrs[ZB] = XY_Data_Json[i][ZB];
        var fea = new SuperMap.Feature.Vector(point, attrs);
        heatFeatures.push(fea);
    }
    XY_GridLayer.radius = 30;
    XY_GridLayer.colors = colors;
    XY_GridLayer.featureWeight = ZB;
    XY_GridLayer.addFeatures(heatFeatures);
}

function Draw_AreaLayer() {
    Area_Layer.removeAllFeatures();
    var ArrLng = [];
    var ArrLat = [];
    for (var i = 0; i < Area_Geo.length; i++) {
        var AreaFea = new SuperMap.Feature.Vector();
        var geo = Area_Geo[i].geometry;
        AreaFea.geometry = geo;
        AreaFea.style = Area_StyleOptions;
        /*添加在地图上显示区域的名称*/
        var ArrStyle = JSON.parse(JSON.stringify(Area_StyleOptions));
        ArrStyle.label = Area_Geo[i].attributes["QH_NAME"];
        AreaFea.style = ArrStyle;
//-------------------------------------------------------------
        var attrs = {};
        attrs["text"] = Area_Geo[i].attributes["QH_NAME"];
        AreaFea.attributes = attrs;

        Area_Layer.addFeatures([AreaFea]);
        ArrLng.push(geo.bounds.left);
        ArrLng.push(geo.bounds.right);
        ArrLat.push(geo.bounds.top);
        ArrLat.push(geo.bounds.bottom);

    }
    if (ArrLng.length > 0) {
        SetMapBound(ArrLng, ArrLat);
    }
}

//清除图层
function ClearAllLayer() {
    //Area_Layer.removeAllFeatures();
    // CY_GridLayer.removeAllFeatures();
    // SY_GridLayer.removeAllFeatures();
    // XY_GridLayer.removeAllFeatures();
    // CY_Data_Json = [];
    // SY_Data_Json = [];
    // XY_Data_Json = [];
    // CYLQParams_Json.sycode = "";
    // CYLQParams_Json.xycode = "";
    // if (HY_Tree_Sel_Node != null) {
    //     $('#HYTree').treeview('unselectNode', HY_Tree_Sel_Node, {
    //         silent: true
    //     });
    //     HY_Tree_Sel_Node = null;
    // }
    document.location.reload();
}



function BackIndex() {
    window.history.back();
}
//地理标签
function Show_DLBQ() {
    if ($('#Show_DLBQ').is(':checked')) {
        //CYJJ_LabelLayer.setVisibility(true);
        for (var i = 0; i < Area_Layer.features.length; i++) {
            var ArrStyle = JSON.parse(JSON.stringify(Area_StyleOptions));
            ArrStyle.label = Area_Layer.features[i].attributes["text"];
            Area_Layer.features[i].style = ArrStyle;
        }
    }
    else {
        //CYJJ_LabelLayer.setVisibility(false);
        for (var i = 0; i < Area_Layer.features.length; i++) {
            var ArrStyle = JSON.parse(JSON.stringify(Area_StyleOptions));
            Area_Layer.features[i].style = ArrStyle;
        }
    }
    Area_Layer.redraw();
}

var realTop=null;
var realLeft =null ;
function takeScreenshot() {
    x_y_width_height();
    var screen_height = screen.height;
    var screen_width = screen.width;
    var MapHeight = document.getElementById("map").scrollHeight;
    var x = (realLeft.toString()).split('.')[0];
    var y = (realTop.toString()).split('.')[0];

    var width =screen_width-360;
    $.ajax({
        url: "/app/takeScreenshot/takeScreenshot/",
        type: 'post',
        data: {x:x,y:y,width:width,height:MapHeight},
        cache: false,
        success: function (text) {

            var str = "data:image/png;base64,"+text;
            var alink = document.createElement("a");
            alink.href =  str;
            alink.download = "cydt.png";
            alink.click();
        },
        error: function (jqXHR, textStatus, errorThrown) {
        }
    });
}
//计算需要截取区域的左上角 距左边和顶部的距离

function x_y_width_height() {

    var BKHeight = window.outerHeight - window.innerHeight;
    var BKWidth =  window.innerWidth- window.outerWidth ;
    var Top = window.screenTop ? window.screenTop : window.screenY;
    var Left = window.screenLeft ? window.screenLeft : window.screenX;
    var Height = document.body.scrollHeight;
    var Width = document.body.scrollWidth;

    if (!!window.ActiveXObject || "ActiveXObject" in window) {
        realTop = Top + 130;   //    56   表示菜单栏的height
        realLeft = Left + BKWidth / 2+360;
    }
    else {
        realTop = Top + BKHeight + 130;
        realLeft = Left + BKWidth / 2+360;
    }
}



